package src.E_Thread;

// 观察线程状态（这里演示了4种状态，还有阻塞(BLOCKED)、等待(WAITING)两种没演示）
public class E12_ThreadState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 2; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        // 观察状态
        Thread.State state = thread.getState();
        System.out.println(state); // NEW

        // 观察启动后
        thread.start();
        state = thread.getState();
        System.out.println(state); // RUNNABLE

        while (state != Thread.State.TERMINATED){ // 只要线程不停止就一直获取状态
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);
        }

        // thread.start(); // 一条线程 终止(TERMINATED) 后就不能再启动了，生命周期结束
    }
}
